<html>
<head>
<title>Threads of Control</title>
</head>
<body>
<table width=100%>
<tr>
<td align=left>
<a href="../cmdLineArgs/index.html"><img src=../../images/PreviousArrow.gif width=26 height=26 align=bottom border=0 alt="Previous | "></a><a
href="definition.html"><img src=../../images/NextArrow.gif width=26 height=26 align=bottom border=0 alt="Next | "></a><a
href="../../index.html"><img src=../../images/WayUpArrow.gif width=26 height=26 align=bottom border=0 alt="Trail Map | "></a><a
href="../index.html"><img src=../../images/javaHeader.gif width=26 height=26 align=bottom border=0 alt="Writing Java Programs | "></a>
<td>
<td align=right>
<a href="TOC.html"><strong><em>Table of Contents</em></strong></a>
</td>
</tr>
</table>
<p>
<hr size=4>

<h1>
     Threads of Control
</h1>
<p>
<blockquote>

Below are three applets from <a href="doc:/demo">HotJava Demos</a>
that animate sorting algorithms.
No, this lesson is not about sorting algorithms. But these
applets do provide a visual aid to understanding a powerful
capability of the Java language--threads.
<pre>
                  <strong>Bi-Directional</strong>
<strong>Bubble Sort</strong>        <strong>Bubble Sort</strong>      <strong>Quick Sort</strong>
<applet codebase=betaclasses code=SortItem.class width=100 height=100><param name="alg" value="BubbleSort"><app class="SortItem" alg="BubbleSort"></applet>     <applet
codebase=betaclasses code=SortItem.class width=100 height=100><param name="alg" value="BidirectionalBubbleSort"><app class="SortItem" alg="BidirectionalBubbleSort"></applet>     <applet
codebase=betaclasses code=SortItem.class width=100 height=100><param name="alg" value="QSort"><app class="SortItem" alg="QSort"></applet>
</pre>
Now start each of the applets, one by one, by clicking on
them with the mouse. Notice anything? Yes! The applets are
running side by side at the same time! Notice anything else?
Yes! You can also scroll this page or bring up one of your browser's
panels at the same time that the three sorting applets sort their data.
All of this is due to the power of <em>threads</em>.

<h4><a href=definition.html>What Are Threads?</a>
</h4>
<blockquote>
A thread--sometimes known as an <em>execution context</em> or 
a <em>lightweight process</em>--is a single sequential flow of control
within a process.
</blockquote>

<h4><a href=simple.html>A Simple Thread Example</a>
</h4>
<blockquote>
The following program is a simple Java application that creates and starts two
independent threads.
<blockquote>
<pre>
class TwoThreadsTest {
    public static void main (String args[]) {
        new SimpleThread(&quot;Jamaica&quot;).start();
        new SimpleThread(&quot;Fiji&quot;).start();
    }
}
class SimpleThread extends Thread {
    public SimpleThread(String str) {
	super(str);
    }
    public void run() {
	for (int i = 0; i &lt; 10; i++) {
	    System.out.println(i + &quot; &quot; + getName());
            try {
		sleep((int)(Math.random() * 1000));
	    } catch (InterruptedException e) {}
	}
	System.out.println(&quot;DONE! &quot; + getName());
    }
}
</pre>
</blockquote>
</blockquote>


<h4><a href=attributes.html>Thread Attributes</a>
</h4>
<blockquote>
To use threads efficiently and without errors you must understand
various aspects of threads and the Java runtime system. You need to
know how to provide a body for a thread, the life-cycle of a thread,
how the runtime system schedules threads, thread groups, and what
daemon threads are and how to write them.
</blockquote>

<h4><a href=multithreaded.html>Mulithreaded Programs</a>
</h4>
<blockquote>
Up until now all of the sample programs in this lesson have used
either one thread or multiple independent threads that run asynchronously.
However, it is often useful to use multiple threads that share data
and therefore must run synchronously. Typically, programs that use
multiple synchronous threads are called multithreaded programs and
require special handling.
</blockquote>

<!-- Add Philosophy of Threads section
Show a good/bad use of threads. When's a good time to use threads.
Debugging threads.
-->

<h4><a href=summary.html>Summary</a>
</h4>
<blockquote>
This lesson has taken you through the intricacies of Java threads
including the life-cycle of a Java thread, scheduling, thread groups,
and synchronization. The Java development environment supports multithreaded
programs through the language, the libraries, and the runtime system.
This summary page highlights all of the features in the Java development
environment that supports threads and gives you links to further documentation
about those features.
</blockquote>

</blockquote>
<p>
<hr size=4>
<p>
<table width=100%>
<tr>
<td align=left>
<a href="../cmdLineArgs/index.html"><img src=../../images/PreviousArrow.gif width=26 height=26 align=top border=0 alt="Previous | "></a><a
href="definition.html"><img src=../../images/NextArrow.gif width=26 height=26 align=top border=0 alt="Next | "></a><a
href="../../index.html"><img src=../../images/WayUpArrow.gif width=26 height=26 align=top border=0 alt="Trail Map | "></a><a
href="../index.html"><img src=../../images/javaHeader.gif width=26 height=26 align=top border=0 alt="Writing Java Programs | "></a>
<td>
<td align=right>
<a href="TOC.html"><strong><em>Table of Contents</em></strong></a>
</td>
</tr>
</table>
</body>
</html>
